package com.hankol.newretail.fragment

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.baselibrary.utils.StaggeredItemDecoration
import com.baselibrary.activity.BaseFragment
import com.hankol.newretail.R
import com.hankol.newretail.adapter.details.GoodsPlantInfoAdapter
import com.hankol.newretail.databinding.FragmentGoodsPlantBinding
import com.hankol.newretail.viewmodel.GoodsDetailViewModel
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
/**
 * @description 种草秀
 * @author 关忆北、
 * @time 2022/8/30 9:47 上午
 */
class GoodsPlantFragment() : BaseFragment() {
    private var pager: Int = 1
    private var goodsId: Long = 0
    private lateinit var viewModel: GoodsDetailViewModel
    private lateinit var dataBinding: FragmentGoodsPlantBinding
    private val mAdapter: GoodsPlantInfoAdapter by lazy { GoodsPlantInfoAdapter() }

    companion object {
        @JvmStatic fun newInstance(goodsId: Long) = GoodsPlantFragment().apply {
            this.goodsId = goodsId
        }
    }


    override fun getLayoutId(): Int {
        return R.layout.fragment_goods_plant
    }

    override fun init() {
        dataBinding = getDataBinding<FragmentGoodsPlantBinding>()
        viewModel = getFragmentViewModelProvider(this).get(GoodsDetailViewModel::class.java)
        initData()
        loadData()
        initListener()
        initObserve()
    }

    fun initData() {
        val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        layoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE
        dataBinding.rvPlantInfoList.addItemDecoration(StaggeredItemDecoration(context, 5f, false))
        dataBinding.rvPlantInfoList.itemAnimator = null
        dataBinding.rvPlantInfoList.layoutManager = layoutManager
        dataBinding.rvPlantInfoList.isNestedScrollingEnabled = true
        dataBinding.rvPlantInfoList.adapter = mAdapter
    }

    fun loadData() {
        viewModel.reqGoodsGrassShow(goodsId, pager)
    }

    fun initListener() {
        dataBinding.srlPlantInfo.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                pager = 1
                viewModel.reqGoodsGrassShow(goodsId, pager)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.reqGoodsGrassShow(goodsId, pager)
            }

        })
    }

    private fun initObserve() {
        viewModel.goodsGrassShow.observe(this, Observer {
            dataBinding.srlPlantInfo.finishRefresh()
            dataBinding.srlPlantInfo.finishLoadMore()
            if (it != null && it.data.isNotEmpty()) {
                if (pager <= 1) {
                    mAdapter.setNewData(it.data)
                } else {
                    mAdapter.addData(it.data)
                }
                pager++
            }
        })
    }

}